set encoding=UTF-8

" Note: Make sure the function is defined before `vim-buffet` is loaded.
function! g:BuffetSetCustomColors()
  hi! BuffetCurrentBuffer cterm=NONE ctermbg=5 ctermfg=8 guibg=#a0e0a0 guifg=#000000
  hi! BuffetTab cterm=NONE ctermbg=4 ctermfg=8 guibg=#6084F0 guifg=#ffffff
endfunction

let g:buffet_powerline_separators = 1
let g:buffet_tab_icon = "\uf00a"
let g:buffet_left_trunc_icon = "\uf0a8"
let g:buffet_right_trunc_icon = "\uf0a9"

call plug#begin('~/.vim/plugged')

" comment
Plug 'tpope/vim-commentary'

" motion
Plug 'easymotion/vim-easymotion'

Plug 'maksimr/vim-jsbeautify'

" formatter
Plug 'Chiel92/vim-autoformat'
let g:python3_host_prog="/usr/bin/python3"

" color schemes
Plug 'altercation/vim-colors-solarized'
Plug 'trevordmiller/nova-vim'
let g:nova_transparent = 1
Plug 'morhetz/gruvbox'
Plug 'tomasr/molokai'
let g:rehash256 = 1

Plug 'sonph/onehalf'
Plug 'NLKNguyen/papercolor-theme'

" fish
Plug 'dag/vim-fish'

" markdown
Plug 'godlygeek/tabular', { 'for': 'markdown' }
Plug 'plasticboy/vim-markdown', { 'for': 'markdown' }

let g:vim_markdown_toc_autofit = 1

" nginx
Plug 'chr4/nginx.vim', { 'for': 'nginx' }

" coc.nvim, coc-clangd need
Plug 'neoclide/coc.nvim', {'branch': 'release'}

" clang-format
Plug 'kana/vim-operator-user'
Plug 'rhysd/vim-clang-format'
let g:clang_format#auto_format=0
" let g:clang_format#code_style=google

" c++
Plug 'jackguo380/vim-lsp-cxx-highlight'

" complementary pairs of mappings.
Plug 'tpope/vim-unimpaired'

" Gundo.vim is Vim plugin to visualize your Vim undo tree.
Plug 'sjl/gundo.vim'
let g:gundo_prefer_python3=1

" 方便处理单词大小写等情况
Plug 'tpope/vim-abolish'

" Brings you the IDE-like tabs into Vim, for easy navigation, and a nice, customizable look
Plug 'bagrat/vim-buffet'

" for devicons
Plug 'ryanoasis/vim-devicons'

" cmake syntax highlight
Plug 'pboettch/vim-cmake-syntax'

" file tree
if has('nvim')
    Plug 'shougo/defx.nvim', {'do': ':UpdateRemotePlugins'}
else
    Plug 'Shougo/defx.nvim'
    Plug 'roxma/nvim-yarp'
    Plug 'roxma/vim-hug-neovim-rpc'
endif

Plug 'kristijanhusak/defx-icons'

Plug 'kristijanhusak/defx-git'

Plug 'liuchengxu/vim-clap', { 'do': ':Clap install-binary' }

" typescript
Plug 'leafgarland/typescript-vim'

" 彩色括号
Plug 'luochen1990/rainbow'
let g:rainbow_active = 1

" svelte
Plug 'leafOfTree/vim-svelte-plugin'
let g:vim_svelte_plugin_use_typescript = 1

call plug#end()

let g:defx_icons_enable_syntax_highlight = 1

let mapleader = ","

autocmd FileType defx call s:defx_my_settings()
function! s:defx_my_settings() abort
    " Define mappings
    setl nonu
    nnoremap <silent><buffer><expr> <CR>
                \ defx#is_directory() ?
                \ defx#do_action('open_or_close_tree') :
                \ defx#do_action('drop',)
    nnoremap <silent><buffer><expr> c
                \ defx#do_action('copy')
    nnoremap <silent><buffer><expr> K
                \ defx#do_action('new_directory')
    nnoremap <silent><buffer><expr> e
                \ defx#do_action('new_file')
    nnoremap <silent><buffer><expr> m
                \ defx#do_action('move')
    nnoremap <silent><buffer><expr> p
                \ defx#do_action('paste')
    nnoremap <silent><buffer><expr> h
                \ defx#do_action('cd', ['..'])
    nnoremap <silent><buffer><expr> l
                \ defx#is_directory() ?
                \ defx#do_action('open') :
                \ defx#do_action('drop',)
    nnoremap <silent><buffer><expr> E
                \ defx#do_action('open', 'vsplit')
    nnoremap <silent><buffer><expr> d
                \ defx#do_action('remove')
    nnoremap <silent><buffer><expr> r
                \ defx#do_action('rename')
    nnoremap <silent><buffer><expr> yy
                \ defx#do_action('yank_path')
    nnoremap <silent><buffer><expr> C
                \ defx#do_action('toggle_columns',
                \                'mark:indent:icons:git:filename:type:size:time')
    nnoremap <silent><buffer><expr> S
                \ defx#do_action('toggle_sort', 'time')
    nnoremap <silent><buffer><expr> !
                \ defx#do_action('execute_command')
    nnoremap <silent><buffer><expr> .
                \ defx#do_action('toggle_ignored_files')
    nnoremap <silent><buffer><expr> ;
                \ defx#do_action('repeat')
    nnoremap <silent><buffer><expr> q
                \ defx#do_action('quit')
endfunction

call defx#custom#option('_', {
            \ 'winwidth': 30,
            \ 'split': 'vertical',
            \ 'direction': 'topleft',
            \ 'show_ignored_files': 0,
            \ 'buffer_name': '',
            \ 'toggle': 1,
            \ 'resume': 1
            \ })

call defx#custom#column('git', {
            \ 'max_width': 4
            \})
call defx#custom#column('filename', {
            \ 'max_width': 30
            \})

" defx
nmap <silent> <leader>e :Defx -columns=mark:git:indent:icons:space:filename:type<cr>

" coc-snippets
" Use <C-l> for trigger snippet expand.
imap <C-l> <Plug>(coc-snippets-expand)

" Use <C-j> for select text for visual placeholder of snippet.
vmap <C-j> <Plug>(coc-snippets-select)

" Use <C-j> for jump to next placeholder, it's default of coc.nvim
let g:coc_snippet_next = '<c-j>'

" Use <C-k> for jump to previous placeholder, it's default of coc.nvim
let g:coc_snippet_prev = '<c-k>'

" Use <C-j> for both expand and jump (make expand higher priority.)
imap <C-j> <Plug>(coc-snippets-expand-jump)

" Goto navigation
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)

" Use <leader>x for convert visual selected code to snippet
xmap <leader>x  <Plug>(coc-convert-snippet)

inoremap jk <esc>

set nu
set expandtab
set tabstop=4
set shiftwidth=4
set smarttab
set hlsearch
filetype indent on
filetype on
filetype plugin on

" code fold, copy from .vimrc
set fdm=indent
set foldlevel=99

" let g:solarized_termcolors=256
set termguicolors
" color solarized
" color default
let g:clap_theme = 'solarized_light'
set background=light
let g:lsp_cxx_hl_light_bg = 1
color PaperColor

" Enable true color 启用终端24位色
if exists('+termguicolors')
  let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
  let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
  set termguicolors
endif

set ignorecase

" custom map
nnoremap - ddp
nnoremap _ kddpk

nnoremap <leader>j :call JsBeautify()<cr>

" comment
nnoremap <leader>/ :Commentary<cr>

" ag
let g:ackprg = 'ag --vimgrep'

" autoformat
nnoremap <leader>f :Autoformat<cr>

" Gundo
nnoremap <leader>g :GundoToggle<cr>

" Clap
nnoremap <C-p> :Clap files<cr>
nnoremap <leader>b :Clap buffers<cr>

" coc-clangd
nnoremap <C-h> :CocCommand clangd.switchSourceHeader <cr>

nnoremap <silent> <leader>n :noh<cr>
nnoremap <leader>w :w<cr>

" color 
nnoremap <leader>h :ColorToggle<cr>

" open vimrc
nnoremap <leader>v :e $MYVIMRC<cr>

" buffet map
nmap <leader>1 <Plug>BuffetSwitch(1)
nmap <leader>2 <Plug>BuffetSwitch(2)
nmap <leader>3 <Plug>BuffetSwitch(3)
nmap <leader>4 <Plug>BuffetSwitch(4)
nmap <leader>5 <Plug>BuffetSwitch(5)
nmap <leader>6 <Plug>BuffetSwitch(6)
nmap <leader>7 <Plug>BuffetSwitch(7)
nmap <leader>8 <Plug>BuffetSwitch(8)
nmap <leader>9 <Plug>BuffetSwitch(9)
nmap <leader>10 <Plug>BuffetSwitch(10)

nmap <leader>c :Bw<cr>
nmap <leader>o :Bonly<cr>

nmap <space>s :CocList symbols<cr>
nmap <space>l :CocList links<cr>
nmap <space>n :CocList location<cr>
nmap <space>o :CocList outline<cr>

nnoremap <space>f :Clap grep2<cr>
nnoremap <space>0 :<C-w>h<cr>
nnoremap <space>1 :<C-w>l<cr>

nnoremap <space>0 <C-w>h
nnoremap <space>1 <C-w>l

nnoremap <leader>t :Toc<cr>

" insert time
nnoremap <leader>dt :pu =strftime(\"%Y-%m-%d %H-%M:%S\")<cr>

" cursor
set cursorline
set colorcolumn=100
